Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update format string for json logger #42

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

oguizol
Copy link

@oguizol oguizol commented Dec 10, 2021

No description provided.

@oguizol
Copy link
Author

oguizol commented Dec 10, 2021

Solves the same issue that this PR but in a different manner

abraggins-vadc pushed a commit to pulse-vadc/prometheus-pgbouncer-exporter that referenced this pull request May 4, 2022
abraggins-vadc pushed a commit to pulse-vadc/prometheus-pgbouncer-exporter that referenced this pull request May 5, 2022
@r-tierney
Copy link

r-tierney commented May 14, 2022

I ran into this following error during the test stage. Found its due to not using the % format style which this PR would fix

======================================================================
ERROR: testSignalHandling (tests.test_cli.TestCLI)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/lib/python3.8/unittest/mock.py", line 1325, in patched
    return func(*newargs, **newkeywargs)
  File "/home/ryant/prometheus-pgbouncer-exporter/.pybuild/cpython3_3.8_prometheus-pgbouncer-exporter/build/tests/test_cli.py", line 89, in testSignalHandling
    cli.main()
  File "/home/ryant/prometheus-pgbouncer-exporter/.pybuild/cpython3_3.8_prometheus-pgbouncer-exporter/build/prometheus_pgbouncer_exporter/cli.py", line 47, in main
    formatter = jsonlogger.JsonFormatter("(asctime) (levelname) (message)", datefmt="%Y-%m-%d %H:%M:%S")
  File "/usr/lib/python3/dist-packages/pythonjsonlogger/jsonlogger.py", line 112, in __init__
    logging.Formatter.__init__(self, *args, **kwargs)
  File "/usr/lib/python3.8/logging/__init__.py", line 580, in __init__
    self._style.validate()
  File "/usr/lib/python3.8/logging/__init__.py", line 433, in validate
    raise ValueError("Invalid format '%s' for '%s' style" % (self._fmt, self.default_format[0]))
ValueError: Invalid format '(asctime) (levelname) (message)' for '%' style

----------------------------------------------------------------------
Ran 32 tests in 0.020s

FAILED (errors=1)
E: pybuild pybuild:341: test: plugin distutils failed with: exit code=1: cd /home/ryant/prometheus-pgbouncer-exporter/.pybuild/cpython3_3.8_prometheus-pgbouncer-exporter/build; python3.8 -m unittest discover -v
dh_auto_test: error: pybuild --test -i python{version} -p 3.8 returned exit code 13
make: *** [debian/rules:7: build] Error 13
dpkg-buildpackage: error: debian/rules build subprocess returned exit status 2

I also found related info for this issue here:

Tested using the % format style:

ryant@pop-os:~ ➜ cat test.py
#!/usr/bin/env python3

import logging
from pythonjsonlogger import jsonlogger

logger = logging.getLogger()
logHandler = logging.StreamHandler()
formatter = jsonlogger.JsonFormatter("%(asctime)s %(levelname)s %(message)s")
logHandler.setFormatter(formatter)
logger.handlers = [logHandler]
logger.setLevel(logging.INFO)

logger.info("Hello world")
ryant@pop-os:~ ➜ ./test.py
{"asctime": "2022-05-14 21:39:12,363", "levelname": "INFO", "message": "Hello world"}
ryant@pop-os:~ ➜

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants